import json
import time
from selenium import webdriver
from appium import webdriver as app_driver
from selenium.webdriver.common.by import By

class UtilsDriver:
    # 定义私有属性，用于存储浏览器驱动
    _mp_driver = None  # 自媒体运营系统浏览器驱动
    _mis_driver = None  # 后台管理系统浏览器驱动
    _app_driver = None  # App用户端浏览器驱动

    # 定义退出后台管理系统浏览器驱动的标识
    quit_mis_driver = True

    # 定义修改私有属性的方法
    @classmethod
    def set_quit_driver(cls, mark):
        cls.quit_mis_driver = mark

    # 定义获取自媒体运营系统浏览器驱动的方法
    @classmethod
    def get_mp_driver(cls):
        if cls._mp_driver is None:
            cls._mp_driver = webdriver.Chrome()
            cls._mp_driver.maximize_window()
            cls._mp_driver.get("http://pc-toutiao-python.itheima.net/#/login")
        return cls._mp_driver

    # 定义退出自媒体运营系统浏览器驱动的方法
    @classmethod
    def quit_mp_driver(cls):
        if cls._mp_driver is not None:
            cls._mp_driver.quit()
            cls._mp_driver = None

    # 定义获取后台管理系统浏览器驱动的方法
    @classmethod
    def get_mis_driver(cls):
        if cls._mis_driver is None:
            cls._mis_driver = webdriver.Chrome()
            cls._mis_driver.maximize_window()
        return cls._mis_driver

    # 定义退出后台管理系统浏览器驱动的方法
    @classmethod
    def quit_mis_driver(cls):
        if cls._mis_driver and cls.quit_mis_driver:
            cls._mis_driver.quit()
            cls._mis_driver = None

    # 定义获取App用户端浏览器驱动的方法
    @classmethod
    def get_app_driver(cls):
        if cls._app_driver is None:
            des_cap = {
                "platformName": "android",
                "platformVersion": "8.0",
                "deviceName": "emulator-5554",
                "appPackage": "org.chromium.webview_shell",
                "appActivity": ".WebviewBrowserActivity",
                "noReset": True,
                "resetKeyboard": True,
                "unicodeKeyboard": True
            }
            cls._app_driver = app_driver.Remote("http://127.0.0.1:4723/wd/hub", des_cap)
        return cls._app_driver

    # 定义退出App用户端浏览器驱动的方法
    @classmethod
    def quit_app_driver(cls):
        if cls._app_driver is not None:
            cls._app_driver.quit()
            cls._app_driver = None

    # 封装选择自媒体运营系统频道的方法
    @staticmethod
    def choice_channel(driver, element, channel):
        element.click()
        time.sleep(1)
        xpath = f"//*[@class='el-select-dropdown__wrap el-scrollbar__wrap']//*[text()='{channel}']"
        driver.find_element(By.XPATH, xpath).click()

    # 封装判断元素是否存在的方法
    @staticmethod
    def is_exist(driver, text):
        xpath = f"//*[contains(text(), '{text}')]"
        try:
            time.sleep(2)
            return driver.find_element(By.XPATH, xpath)
        except Exception as e:
            return False

    # 封装获取测试数据的方法
    @staticmethod
    def get_case_data(filename):
        with open(filename, encoding='utf-8') as f:
            case = json.load(f)
        list_case_data = [tuple(case_data.values()) for case_data in case.values()]
        return list_case_data

    # 封装App中边滑动边查找的方法
    @staticmethod
    def app_swipe_find(driver, element, target_ele):
        location = element.location
        x = location["x"]
        y = location["y"]
        size = element.size
        width = size["width"]
        height = size["height"]
        start_x = x + width * 0.9
        end_y = y + height * 0.5
        end_x = x + width * 0.1

        while True:
            page_source = driver.page_source
            try:
                time.sleep(2)
                driver.find_element(*target_ele).click()
                return True
            except Exception as e:
                driver.swipe(start_x, end_y, end_x, end_y, duration=1500)
            if page_source == driver.page_source:
                print("已滑屏到最后一页，没有找到对应频道！")
                return False